package cloud.shyidong.core.util;

import cloud.shyidong.core.exception.BusinessException;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import freemarker.template.Configuration;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Map;

/**
 * @Description TODO
 * @Author syd
 * @Date 2022/9/27
 */
@Slf4j
public class CmsGenerateUtil {
    
    private static Configuration configuration = null;
    
    public static void generateHtml(String templateFile, String targetPath, String targetFile, Map<String, Object> params) {
        if (log.isDebugEnabled()) {
            log.debug("templateFile >>> {}, targetPath >>> {}, targetFile >>> {}", templateFile, targetPath, targetFile);
            for (String key : params.keySet()) {
                log.debug("{} >>> {}", key, params.get(key));
            }
        }
        Writer writer = null;
        try {
            if (configuration == null) {
                String path = SpringUtils.getProperty("file.template.path");
                if (log.isDebugEnabled()) {
                    log.debug("path >>> {}", path);
                }
                configuration = new Configuration(Configuration.VERSION_2_3_28);
                configuration.setDirectoryForTemplateLoading(new File(path));
                configuration.setDefaultEncoding("utf-8");
            }
            Template template = configuration.getTemplate(templateFile);
            StringBuilder filePath = new StringBuilder(SpringUtils.getProperty("file.portal.path"));
            filePath.append("html");
            filePath.append(File.separator);
            filePath.append(targetPath);
            File file = new File(filePath.toString());
            if (!file.exists()) {
                file.mkdirs();
            }
            filePath.append(File.separator);
            if (ObjectUtils.isNull(targetFile)) {
                filePath.append("index");
            } else {
                filePath.append(targetFile);
            }
            filePath.append(".html");
            file = new File(filePath.toString());
            if (!file.exists()) {
                file.createNewFile();
            }
            writer = new FileWriter(file);
            template.process(params, writer);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw BusinessException.from(e.getMessage());
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
    
    public static void generateHtml(String templateFile, String targetPath, Map<String, Object> params) {
        generateHtml(templateFile, targetPath, null, params);
    }
    
    /**
     * 生成源代码
     *
     * @param templateFile
     * @param targetFile
     * @param params
     */
    public static void generateSource(String templateFile, String targetPath, String targetFile, Map<String, Object> params) {
        if (log.isDebugEnabled()) {
            log.debug("templateFile >>> {}, targetPath >>> {}, targetFile >>> {}", templateFile, targetPath, targetFile);
            for (String key : params.keySet()) {
                log.debug("{} >>> {}", key, params.get(key));
            }
        }
        Writer writer = null;
        try {
            if (configuration == null) {
                String path = SpringUtils.getProperty("file.template.path");
                if (log.isDebugEnabled()) {
                    log.debug("path >>> {}", path);
                }
                configuration = new Configuration(Configuration.VERSION_2_3_28);
                configuration.setDirectoryForTemplateLoading(new File(path));
                configuration.setDefaultEncoding("utf-8");
            }
            Template template = configuration.getTemplate(templateFile);
            File filePath = new File(targetPath);
            if (!filePath.exists()) {
                filePath.mkdirs();
            }
            File file = new File(targetPath + targetFile);
            if (!file.exists()) {
                if (log.isDebugEnabled()) {
                    log.debug("newFile >>> {}", file.getAbsolutePath());
                }
                file.createNewFile();
            }
            writer = new FileWriter(file);
            template.process(params, writer);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw BusinessException.from(e.getMessage());
        } finally {
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    log.error(e.getMessage(), e);
                }
            }
        }
    }
    
}
